package com.sxh.user.Utils;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.stereotype.Component;


//参考AI
@Component
public class RocketMQProducerUtil {
    private static final String NAME_SERVER_ADDR = "127.0.0.1:9876";
    private static final String PRODUCER_GROUP = "szml";
    private static DefaultMQProducer producer;
    static {
        // 初始化生产者
        initProducer();
    }
    /**
     * 初始化生产者实例
     */
    private static void initProducer() {
        producer = new DefaultMQProducer(PRODUCER_GROUP);
        producer.setNamesrvAddr(NAME_SERVER_ADDR);
        producer.setSendMsgTimeout(3000);
        producer.setRetryTimesWhenSendFailed(3);
        try {
            producer.start();
            System.out.println("RocketMQ生产者初始化成功");
            // 注册JVM关闭钩子，优雅关闭生产者
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                if (producer != null) {
                    producer.shutdown();
                    System.out.println("RocketMQ生产者已关闭");
                }
            }));
        } catch (MQClientException e) {
            System.err.println("RocketMQ生产者初始化失败: " + e.getMessage());
            throw new RuntimeException("RocketMQ生产者初始化失败", e);
        }
    }
    /**
     * 发送普通异步消息
     * @param tag 操作类型
     * @param message 消息内容
     * @param callback 回调函数
     */
    public static void sendMessageAsync(String tag, String message, SendCallback callback) {
        try {
            Message msg = new Message("UserOption", tag, message.getBytes(RemotingHelper.DEFAULT_CHARSET));
            producer.send(msg, callback);
            System.out.printf("异步消息已提交 - 主题: %s, 标签: %s%n", "UserOption", tag);
        } catch (Exception e) {
            System.err.printf("异步消息提交失败 - 主题: %s, 标签: %s, 错误: %s%n",
                    "UserOption", tag, e.getMessage());
            throw new RuntimeException("异步消息提交失败", e);
        }
    }

//    /**
//     * 发送带键的异步消息
//     * @param topic 主题
//     * @param tag 标签
//     * @param keys 业务键
//     * @param message 消息内容
//     * @param callback 回调函数
//     */
//    public static void sendMessageAsync(String topic, String tag, String keys, String message, SendCallback callback) {
//        try {
//            Message msg = new Message(topic, tag, keys, message.getBytes(RemotingHelper.DEFAULT_CHARSET));
//            producer.send(msg, callback);
//            System.out.printf("带键异步消息已提交 - 主题: %s, 标签: %s, 键: %s%n", topic, tag, keys);
//        } catch (Exception e) {
//            System.err.printf("带键异步消息提交失败 - 主题: %s, 标签: %s, 键: %s, 错误: %s%n",
//                    topic, tag, keys, e.getMessage());
//            throw new RuntimeException("带键异步消息提交失败", e);
//        }
//    }
    /**
     * 私有构造函数，防止实例化工具类
     */
    private RocketMQProducerUtil() {}
}
